package com.beerandjesus.blackjack.deck;

/**
 * User: reverend
 * Date: Jul 1, 2005
 * Time: 1:12:01 AM
 */
public class Card {

    public static final Card JOKER = new Card(0, new Suit("Joker"));
    public static final CardValue JACK = new CardValue(11);
    public static final CardValue QUEEN = new CardValue(12);
    public static final CardValue KING = new CardValue(13);
    public static final CardValue ACE = new CardValue(1);

    private CardValue cardValue;
    private Suit suit;

    public Card(int value, Suit suit) {
        this.cardValue = new CardValue(value);
        this.suit = suit;
    }

    public Card(CardValue value, Suit suit) {
        this.cardValue = value;
        this.suit = suit;
    }

    public boolean equals(Object o) {
        if (!(o instanceof Card)) {
            return false;
        }
        Card card = (Card)o;
        return (card.getFaceValue() == this.getFaceValue() && card.getSuit().equals(this.suit));
    }

    public int getFaceValue() {
        return cardValue.faceValue();
    }

    public Suit getSuit() {
        return this.suit;
    }

    public int getValue() {
        return this.cardValue.getValueForBlackjack();
    }

    private static int aceValueForBlackjack() {
        return 1;
    }

    public boolean isAce() {
        return cardValue.isAce();
    }

    public String getDisplayString() {
        String value;
        switch (cardValue.faceValue()) {
            case 1:
                value = "Ace";
                break;
            case 11:
                value = "Jack";
                break;
            case 12:
                value = "Queen";
                break;
            case 13:
                value = "King";
                break;
            default:
                value = String.valueOf(cardValue.faceValue());
        }
        return value + " of " + suit.getTypePlural();
    }


    private static class CardValue {
        public int faceValue;

        public CardValue(int value) {
            this.faceValue = value;
        }

        public int getValueForBlackjack() {
            switch (faceValue) {
                case 13:
                    return 10;
                case 12:
                    return 10;
                case 11:
                    return 10;
                case 1:
                    return aceValueForBlackjack();
                default:
                    return faceValue;
            }

        }

        public int faceValue() {
            return faceValue;
        }

        public boolean isAce() {
            return faceValue == 1;
        }
    }
}
